package HOT;

import java.util.HashMap;
import java.util.Map;

/**
 * Author  zxy
 * Date  2023/6/1 下午 4:22
 */

class TrieNode {
    Map<Character, TrieNode> map;
    boolean isEnd;

    public TrieNode() {
        map = new HashMap<>();
        isEnd = false;
    }
}

public class Trie {
    TrieNode root;

    public Trie() {
        root = new TrieNode();
    }

    public void insert(String word) {

        TrieNode node = root;
        for (Character c : word.toCharArray()) {
            if (!node.map.containsKey(c)) {
                node.map.put(c, new TrieNode());
            }

            node = node.map.get(c);
        }

        node.isEnd = true;
    }

    public boolean search(String word) {

        TrieNode node = root;
        for (Character c : word.toCharArray()) {
            if (!node.map.containsKey(c)) {
                return false;
            }
            node = node.map.get(c);
        }

        return node.isEnd;
    }

    public boolean startsWith(String prefix) {

        TrieNode node = root;
        for (Character c : prefix.toCharArray()) {
            if (!node.map.containsKey(c)) {
                return false;
            }
            node = node.map.get(c);
        }

        return true;
    }
}

/**
 * Your HOT.Trie object will be instantiated and called as such:
 * HOT.Trie obj = new HOT.Trie();
 * obj.insert(word);
 * boolean param_2 = obj.search(word);
 * boolean param_3 = obj.startsWith(prefix);
 */